package com.s7.app;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.s7.app.manager.AppManager;

/**
 * Created by Administrator on 2018\3\1 0001.
 * Activity基础类
 */
public abstract class CompatActivity extends AppCompatActivity {

    protected String LOG_TAG = this.getClass().getSimpleName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AppManager.getInstance().add(this);
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        // 是否触发按键为back键
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            onBackPressed();
//            return true;
//        }
//        // 如果不是back键正常响应
//        return super.onKeyDown(keyCode, event);
//    }

    @Override
    public void onBackPressed() {
        AppManager.getInstance().remove(this);
        super.onBackPressed();
    }

    /**
     * 获取Bundle对象
     * @return
     */
    public Bundle getBundle() {
        Bundle bundle = getIntent().getBundleExtra("bundle");
        return bundle;
    }

    /**
     * 自定义 Activity 跳转间隔动画
     */
    public void onLoadingAnim() {

    }

    /**
     * 通过Class跳转界面
     **/
    protected void startActivity(Class<?> cls) {
        startActivity(cls, null);
    }

    /**
     * 含有Bundle通过Class跳转界面
     **/
    protected void startActivity(Class<?> cls, Bundle bundle) {
        onLoadingAnim();
        Intent intent = new Intent();
        intent.setClass(this, cls);
        if (bundle != null) {
            intent.putExtra("bundle", bundle);
        }
        startActivity(intent);
    }

    /**
     * 通过Class跳转界面
     * 关闭时获得该Class页面的返回参数
     **/
    protected void startActivityForResult(Class<?> cls, int sign) {
        onLoadingAnim();
        Intent intent = new Intent();
        intent.setClass(this, cls);
        startActivityForResult(intent, sign);
    }

    /**
     * 含有Bundle通过Class跳转界面
     * 关闭时获得该Class页面的返回参数
     **/
    protected void startActivityForResult(Class<?> cls, int sign, Bundle bundle) {
        onLoadingAnim();
        Intent intent = new Intent();
        intent.setClass(this, cls);
        intent.putExtra("bundle", bundle);
        startActivityForResult(intent, sign);
    }

    /**
     * 隐藏View对应的软键盘
     * @param v
     */
    public Boolean hideInputMethod(View v) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            return imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
        return false;
    }

    /**
     * 判断当前点击屏幕的地方是否是软键盘
     * @param view
     * @param event
     * @return
     */
    public boolean isShouldHideInput(View view, MotionEvent event) {
        if(view != null && (view instanceof EditText)) {
            int[] leftTop = {0, 0};
            view.getLocationInWindow(leftTop);
            int left = leftTop[0],
                    top = leftTop[1],
                    bottom = top + view.getHeight(),
                    right = left + view.getWidth();
            if (event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom) {
                /**
                 * 保留点击EditText的事件
                 */
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    /**
     * 关闭当前Activity，并从盏中移除
     */
    @Override
    public void finish() {
        AppManager.getInstance().remove(this);
        super.finish();
    }

    protected void onExit() {
        AppManager.getInstance().AppExit(this);
    }

}
